<?php
/**
 * phpWord 组件类
 *
 * @author heyijia
 * @date 2018-12-06
 */

namespace app\common\components;

use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Style\TablePosition;
use app\modules\counseling\service\RecordService;
use app\modules\user\service\StudentService;

class Word extends Component
{
    public $phpWord;

    public function init()
    {
        parent::init();
    }
		
		 // 生成申报书
    public function createCounselingRecordWord($recordId, $filePath)
    {
				$recordInfo = (new RecordService)->info([
						'condition' => [
								'status' => 1,
								'id' => $recordId
						],
						'with' => ['teacherInfo', 'tags', 'categorys', 'questions'],
				]);
				$studentInfo = (new StudentService)->info([
						'condition' => [
								'status' => 1,
								'student_id' => $recordInfo['student_id'],
						],
						'with' => ['classInfo', 'gradeInfo', 'collegeInfo', 'instructorInfo'],
				]);
				
				$information['name'] = $studentInfo['name']??'';
				$information['counselor'] = $recordInfo['teacherInfo']['name']??'';
				$information['instructor'] = $studentInfo['instructorInfo']['name']??'';
				$information['counseling_time'] = date("Y-m-d", $recordInfo['create_timestamp']/1000);
				$information['counseling_time2'] = date("Ymd", $recordInfo['create_timestamp']/1000);
				$information['college'] = $studentInfo['collegeInfo']['name']??'';
				$information['grade'] = $studentInfo['gradeInfo']['name']??'';
				$information['class'] = $studentInfo['classInfo']['name']??'';
				
				$information['major'] = '';//专业
				$information['student_id'] = $recordInfo['student_id']??'';
				$information['sex'] = '';//性别
				$information['nationality'] = '';//民族
				$information['phone'] = $studentInfo['contact_info']??'';
				$information['political_status'] = $studentInfo['political_status']??'';
				$information['e_info'] = '';//电子信息
				$information['religious'] = $studentInfo['religious']??'';
				$information['birthplace'] = '';//籍贯
				
				$information['category'] = '';
				$flag = 1;
				foreach($recordInfo['categorys'] as $categoryInfo){
						if($flag == 1){
								$information['category'] = $categoryInfo['categoryInfo']['name'];
								$flag = 0;
						}
						else
								$information['category'] = $information['category'].'，'.$categoryInfo['categoryInfo']['name'];
				}
				
				$information['question'] = [];
				$flag = 1;
				foreach($recordInfo['questions'] as $questionInfo){
						$information['question'][] = '问：'.$questionInfo['questionInfo']['title'];
						$information['question'][] = '答：'.$questionInfo['answer'];
						$information['question'][] = '建议：'.$questionInfo['suggestion'];
				}
				
				$information['tag'] = '';
				$flag = 1;
				foreach($recordInfo['tags'] as $tagInfo){
						if($flag == 1){
								$information['tag'] = $tagInfo['tagInfo']['name'];
								$flag = 0;
						}
						else
								$information['tag'] = $information['tag'].'，'.$tagInfo['tagInfo']['name'];
				}
				
				$information['summary'] = $recordInfo['summary'];
				$information['is_red'] = ($recordInfo['is_counsel_red'] == 1)?'标记红色预警':'未标记红色预警';
				$information['referral_department'] = '建议转介部门：'.$recordInfo['referral_department'];
				
        // 定义style
        $style = [
            'basicBreak' => [
                'font' => array('size' => 18),
                'para' => array('spacing' => 50),
            ],
            'header' => [
                'font' => array('size' => 16, 'bold' => true, 'name' => '黑体'),
                'para' => array('align' => 'center'),
            ],
            'header1' => [
                'font' => array('size' => 24, 'bold' => true, 'name' => '黑体'),
                'para' => array('align' => 'center'),
            ],
            'table1' => [
            	'height' => 1200,
            	'table' => array('cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50),
            	'cell' => array('valign' => 'center'),
            	'left' => [
            		'width' => 2010,
            		'font' => array('size' => 14, 'name' => '黑体'),
            		'para' => array('spacing' => 100),
            	],
            	'right' => [
            		'width' => 4510,
            		'font' => array('size' => 14, 'name' => '黑体', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE),
            		'para' => array('spacing' => 100),
            	],
            ],
            'class' => [
								'font' => array('size' => 14, 'name' => '黑体'),
                'para' => array('indent' => 1.5, 'spacing' => 200),
            ],
            'date' => [
								'font' => array('size' => 14, 'name' => '黑体'),
                'para' => array('align' => 'center', 'spacing' => 200),
            ],
            'decDate' => [
								'font' => array('size' => 12, 'name' => '宋体'),
                'para' => array('align' => 'right', 'spacing' => 50),
            ],
            'mainTable' => [
                'table' => array('cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50, 'borderSize' => 6, 'borderColor' => '000000'),
                'basicFont' => array('size' => 14, 'name' => '宋体'),
                'basicPara' => array('spacing' => 50),
                'centerStyle' => array('spacing' => 50,'align'=>'center'),
                'eight-layer' => [
                    'height' => 600,
                    'width' => 1125,
                    'cell' => array('gridSpan' => 1),
                ],
                'one-seven-layer' => [
                    'height' => 600,
                    'leftWidth' => 1125,
                    'rightWidth' => 7875,
                    'leftCell' => array('gridSpan' => 1),
                    'rightCell' => array('gridSpan' => 7),
                ],
								'one-seven-big-layer' => [
                    'height' => 1800,
                    'leftWidth' => 1125,
                    'rightWidth' => 7875,
                    'leftCell' => array('gridSpan' => 1),
                    'rightCell' => array('gridSpan' => 7),
                ],
                'complex-layer' => [
                    'height' => 600,
                    'leftWidth' => 1125,
                    'leftCell' => array('vMerge' => 'restart', 'gridSpan' => 1, 'valign' => 'center'),
                    'oneWidth' => 1125,
                    'oneCell' => array('gridSpan' => 1, 'valign' => 'center'),
                    'oneMergeCell' => array('vMerge' => 'continue', 'gridSpan' => 1, 'valign' => 'center'),
                    'twoWidth' => 2250,
                    'twoCell' => array('gridSpan' => 2, 'valign' => 'center'),
                    'cellRowContinue' => array('vMerge' => 'continue', 'gridSpan' => 2),
                ],
								'complex2-layer' => [
                    'height' => 600,
                    'leftWidth' => 1125,
                    'leftCell' => array('vMerge' => 'restart', 'gridSpan' => 1, 'valign' => 'center'),
                    'rightWidth' => 7875,
                    'rightCell' => array('gridSpan' => 7, 'valign' => 'center'),
                    'mergeCell' => array('vMerge' => 'continue', 'gridSpan' => 1, 'valign' => 'center'),
                    'cellRowContinue' => array('vMerge' => 'continue', 'gridSpan' => 2),
                ],
            ],
        ];
				
				$this->phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $this->phpWord->addSection();

        // 标题
        $section->addText($information['name'].'_'.$information['student_id'].'_'.$information['counseling_time2'].'深度辅导过程记录', $style['header']['font'], $style['header']['para']);
        $section->addTextBreak(1, $style['basicBreak']['font'], $style['basicBreak']['para']);

        // 项目具体信息表格, 将表格划分成几部分循环(相同样式的在同一部分)
        $mainTable = $section->addTable($style['mainTable']['table']);

        $data = [
            '姓名' => $information['name'],
            '辅导人' => $information['counselor'],
            '辅导员' => $information['instructor'],
            '辅导日期' => $information['counseling_time'],
        ];
        $mainTable->addRow($style['mainTable']['eight-layer']['height']);
        foreach ($data as $key => $value) {
            $mainTable->addCell($style['mainTable']['eight-layer']['width'], $style['mainTable']['eight-layer']['cell'])->addText($key, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
            $mainTable->addCell($style['mainTable']['eight-layer']['width'], $style['mainTable']['eight-layer']['cell'])->addText($value, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        }
        
				$mainTable->addRow($style['mainTable']['complex-layer']['height']);
        $complexStyle = $style['mainTable']['complex-layer'];
				$mainTable->addCell($complexStyle['leftWidth'], $complexStyle['leftCell'])->addText('基本信息', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        $data = [
						[
								'学院' => $information['college'],
								'年级' => $information['grade'],
								'班级' => $information['class'],
						],
						[
								'专业' => $information['major'],
								'学号' => $information['student_id'],
								'性别' => $information['sex'],
						],
						[
								'民族' => $information['nationality'],
								'联系方式' => $information['phone'],
								'政治面貌' => $information['political_status'],
						],
						[
								'电子信息' => $information['e_info'],
								'宗教信仰' => $information['religious'],
								'籍贯' => $information['birthplace'],
						],
				];
				$temp = 0;
				foreach ($data as $array) {
						if($temp == 1){
								$mainTable->addRow($style['mainTable']['complex-layer']['height']);
								$mainTable->addCell($complexStyle['oneWidth'], $complexStyle['oneMergeCell']);
								}
						$temp = 1;
						$flag = 1;
            foreach($array as $key => $value){
								$mainTable->addCell($complexStyle['oneWidth'], $complexStyle['oneCell'])->addText($key, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
								if($flag == 1){
										$mainTable->addCell($complexStyle['twoWidth'], $complexStyle['twoCell'])->addText($value, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
										$flag = 0;
								}
								else
										$mainTable->addCell($complexStyle['oneWidth'], $complexStyle['oneCell'])->addText($value, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
						}
				}
				//辅导方向
				$mainTable->addRow($style['mainTable']['one-seven-layer']['height']);
				$mainTable->addCell($style['mainTable']['one-seven-layer']['leftWidth'], $style['mainTable']['one-seven-layer']['leftCell'])->addText('辅导方向', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        $mainTable->addCell($style['mainTable']['one-seven-layer']['rightWidth'], $style['mainTable']['one-seven-layer']['rightCell'])->addText($information['category'], $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        //基础提问
				$mainTable->addRow($style['mainTable']['one-seven-big-layer']['height']);
				$mainTable->addCell($style['mainTable']['one-seven-big-layer']['leftWidth'], $style['mainTable']['one-seven-big-layer']['leftCell'])->addText('基础提问', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);	
				$cell = $mainTable->addCell($style['mainTable']['one-seven-big-layer']['rightWidth'], $style['mainTable']['one-seven-big-layer']['rightCell']);
				foreach($information['question'] as $questionInfo){
						$cell->addText($questionInfo, $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
				}
				//辅导总结
				$mainTable->addRow($style['mainTable']['one-seven-big-layer']['height']);
				$mainTable->addCell($style['mainTable']['one-seven-big-layer']['leftWidth'], $style['mainTable']['one-seven-big-layer']['leftCell'])->addText('辅导总结', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        $mainTable->addCell($style['mainTable']['one-seven-big-layer']['rightWidth'], $style['mainTable']['one-seven-big-layer']['rightCell'])->addText($information['summary'], $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        //人员标签
				$mainTable->addRow($style['mainTable']['one-seven-layer']['height']);
				$mainTable->addCell($style['mainTable']['one-seven-layer']['leftWidth'], $style['mainTable']['one-seven-layer']['leftCell'])->addText('人员标签', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        $mainTable->addCell($style['mainTable']['one-seven-layer']['rightWidth'], $style['mainTable']['one-seven-layer']['rightCell'])->addText($information['tag'], $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
        
        $mainTable->addRow($style['mainTable']['complex2-layer']['height']);
        $complexStyle = $style['mainTable']['complex2-layer'];
				
				$mainTable->addCell($complexStyle['leftWidth'], $complexStyle['leftCell'])->addText('转介处理', $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
				$mainTable->addCell($complexStyle['rightWidth'], $complexStyle['rightCell'])->addText($information['is_red'], $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
				
				$mainTable->addRow($style['mainTable']['complex2-layer']['height']);
				$mainTable->addCell($complexStyle['rightWidth'], $complexStyle['mergeCell']);
				$mainTable->addCell($complexStyle['rightWidth'], $complexStyle['rightCell'])->addText($information['referral_department'], $style['mainTable']['basicFont'], $style['mainTable']['basicPara']);
				
        $objWriter= \PhpOffice\PhpWord\IOFactory::createWriter($this->phpWord, 'Word2007');

        $objWriter->save($filePath);
    }
}